package cn.xqj.basic.service.impl;

import cn.xqj.basic.mapper.BaseMapper;
import cn.xqj.basic.query.BaseQuery;
import cn.xqj.basic.service.IBaseService;
import cn.xqj.basic.util.PageList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;



@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class BaseServiceImpl<T> implements IBaseService<T>  {

    @Autowired
    private BaseMapper<T> baseMapper;

    @Override
    public List<T> findAll() {
        return baseMapper.findAll();
    }

    @Override
    public T findOne(Long id) {
        return baseMapper.findOne(id);
    }

    @Override
    @Transactional
    public void add(T t) {
        baseMapper.add(t);
    }

    @Override
    @Transactional
    public void del(Long id) {
        baseMapper.del(id);
    }

    @Override
    @Transactional
    public void update(T t) {
        baseMapper.update(t);
    }

    @Override
    public PageList<T> pageQuery(BaseQuery query) {
        // 先查询数据的总条数，用于前端分页查询
        Long count = baseMapper.queryCount(query);
        // 判断总条数是否是0，是0就直接返回一个PageList<T>
        if (count == 0) {
            return new PageList<T>();
        }
        // 查询所有的数据集合
        List<T> ts = baseMapper.queryData(query);
        // 返回一个PageList<T>(count, ts)把总条数和数据聚合传进去
        return new PageList<T>(count, ts);
    }

    @Override
    @Transactional
    public void patchDel(Long[] ids) {
        baseMapper.patchDel(ids);
    }
}
